package com.jmu.bishe.web.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.jmu.bishe.web.common.BaseController;

public class ExceptionResolver extends BaseController implements HandlerExceptionResolver{
	Logger log = Logger.getLogger(ExceptionResolver.class);

	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) {
		log.info("Handle exception:"+ ex.getClass().getName(),ex);

		if(ex instanceof org.springframework.web.multipart.MaxUploadSizeExceededException){
			//此处属于文件大小超过配置的文件大小限制
			String tip =   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
	                "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
	                "<head>\n" +
	                "    <title>畅享信息</title>\n" +
	                "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"+
					"</head><body><script>" +
        			"top.alertTip('系统提示','文件超过系统预定义大小，请重新选择！',function(){" +
        			"			top.closeTip();history.back();" +
        			"		});</script></body></html>";
			
			printOut(response, tip);
			return null;
		}else if(ex instanceof AjaxException){
            String errorMsg = ex.getMessage();
            if(errorMsg==null){
                errorMsg = "内部处理异常！";
            }
            printOut(response, "{\"status\":\"error\",\"errorMsg\":\""+errorMsg+"\"}");
            return null;
        }else{
			ModelAndView model = new ModelAndView("errors/500");
			model.addObject("status", false);
			model.addObject("message", ex.getMessage());
			return model;
		}
	}
}
